<?php
/**
 * @package Admin
 * @author Henrique Droog <henridroog@gmail.com>
 * @name ControllerMapper
 * @since 19/03/2011
 * @version 1.0
 */
class Admin_Model_ControllerMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Admin_Model_DbTable_Controller');
        }
        return $this->_dbTable;
    }

    public function save(Admin_Model_Controller $controller) {
        $data = array(
            'controller_name' => $controller->getName(),
            'module_id' => $controller->getModule(),
        );
        if ($controller->getId()<=0) {
            unset($data['controller_id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('controller_id = ?' => $controller->getId()));
        }
    }

    public function find($id, Admin_Model_Controller $controller) {
        $result = $this->getDbTable()->find($id['id']);

        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $controller->setId($row->controller_id)
                   ->setName($row->controller_name)
                   ->setModule($row->module_id);
        return $controller;
    }

    public function fetchAll() {
        $resultSet = $this->getDbTable()->fetchAll();
        
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_Controller();
            $entry->setId($row->controller_id)
                  ->setName($row->controller_name)
                  ->setModule($row->module_id);
            $entries[] = $entry;
        }
        return $entries;
    }

    //
    public function del(Admin_Model_Controller $controller){
        $where = array("controller_id = ?" =>$controller->getId());
        $resultSet = $this->getDbTable()->delete($where);
        return $resultSet;
    }

    /**
     * Método responsavel por selecionar todos os registros com join na table acl.module
     * @author henrique
     * @name selecionaAll
     * @version 1.0
     * @since 21/03/2011
     */
    public function selecionaAll(){
        $select = $this->getDbTable()->select()
                ->setIntegrityCheck(false)
                ->from(array("co" => "acl.acl_controller"))
                ->join(array("md" => "acl.acl_module"),"md.module_id = co.module_id",array("md.module_name"));

        $resultSet = $this->getDbTable()->fetchAll($select);
        
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_Controller();
            $entry->setId($row->controller_id)
                  ->setName($row->controller_name)
                  ->setModule($row->module_name);
            
            $entries[] = $entry;
        }
        return $entries;
    }

    /**
     *
     */
    public function selecionFiltros(array $data){
        $where = array();
        $select = $this->getDbTable()->select()
                ->setIntegrityCheck(false)
                ->from(array("co" => "acl.acl_controller"))
                ->where("co.module_id = ?",$data['module'])
                ->order("co.controller_name asc");

        $resultSet = $this->getDbTable()->fetchAll($select)->toArray();

        return $resultSet;
    }
}